Conditions | 10 |
Paths | 69 |
Total Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like jQuery.extend.setting.set often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | var kirki = kirki || {}; |
||
69 | set: function( element, value, key ) { |
||
70 | var setting, |
||
71 | parts, |
||
72 | currentNode = '', |
||
73 | foundNode = '', |
||
74 | subSettingObj = {}, |
||
75 | currentVal, |
||
76 | subSetting, |
||
77 | subSettingParts; |
||
78 | |||
79 | // Get the setting from the element. |
||
80 | setting = element; |
||
81 | if ( _.isObject( element ) ) { |
||
82 | if ( jQuery( element ).attr( 'data-id' ) ) { |
||
83 | setting = element.attr( 'data-id' ); |
||
84 | } else { |
||
85 | setting = element.parents( '[data-id]' ).attr( 'data-id' ); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | if ( 'undefined' !== typeof wp.customize.control( setting ) ) { |
||
90 | wp.customize.control( setting ).setting.set( value ); |
||
91 | return; |
||
92 | } |
||
93 | |||
94 | parts = setting.split( '[' ), |
||
95 | |||
96 | // Find the setting we're using in the control using the customizer API. |
||
97 | _.each( parts, function( part, i ) { |
||
98 | part = part.replace( ']', '' ); |
||
99 | |||
100 | // The current part of the setting. |
||
101 | currentNode = ( 0 === i ) ? part : '[' + part + ']'; |
||
102 | |||
103 | // When we find the node, get the value from it. |
||
104 | // In case of an object we'll need to merge with current values. |
||
105 | if ( ! _.isUndefined( wp.customize.instance( currentNode ) ) ) { |
||
106 | foundNode = currentNode; |
||
107 | currentVal = wp.customize.instance( foundNode ).get(); |
||
108 | } |
||
109 | } ); |
||
110 | |||
111 | // Get the remaining part of the setting that was unused. |
||
112 | subSetting = setting.replace( foundNode, '' ); |
||
113 | |||
114 | // If subSetting is not empty, then we're dealing with an object |
||
115 | // and we need to dig deeper and recursively merge the values. |
||
116 | if ( '' !== subSetting ) { |
||
117 | if ( ! _.isObject( currentVal ) ) { |
||
118 | currentVal = {}; |
||
119 | } |
||
120 | if ( '[' === subSetting.charAt( 0 ) ) { |
||
121 | subSetting = subSetting.replace( '[', '' ); |
||
122 | } |
||
123 | subSettingParts = subSetting.split( '[' ); |
||
124 | _.each( subSettingParts, function( subSettingPart, i ) { |
||
125 | subSettingParts[ i ] = subSettingPart.replace( ']', '' ); |
||
126 | } ); |
||
127 | |||
128 | // If using a key, we need to go 1 level deeper. |
||
129 | if ( key ) { |
||
130 | subSettingParts.push( key ); |
||
131 | } |
||
132 | |||
133 | // Converting to a JSON string and then parsing that to an object |
||
134 | // may seem a bit hacky and crude but it's efficient and works. |
||
135 | subSettingObj = '{"' + subSettingParts.join( '":{"' ) + '":"' + value + '"' + '}'.repeat( subSettingParts.length ); |
||
136 | subSettingObj = JSON.parse( subSettingObj ); |
||
137 | |||
138 | // Recursively merge with current value. |
||
139 | jQuery.extend( true, currentVal, subSettingObj ); |
||
140 | value = currentVal; |
||
141 | |||
142 | } else { |
||
143 | if ( key ) { |
||
144 | currentVal = ( ! _.isObject( currentVal ) ) ? {} : currentVal; |
||
145 | currentVal[ key ] = value; |
||
146 | value = currentVal; |
||
147 | } |
||
148 | } |
||
149 | wp.customize.control( foundNode ).setting.set( value ); |
||
150 | } |
||
151 | } |
||
153 |